package com.ciaojian.worker.service;

import com.ciaojian.core.enums.business.*;
import com.ciaojian.core.mapper.ProductGroupMapper;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.ProductGroup;
import com.ciaojian.core.service.ProductGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author oxiny
 */
@Service
public class ProductGroupServiceImpl extends ProductGroupService<ProductGroupMapper, ProductGroup> {
    @Resource
    private WorkerServiceImpl workerService;
    @Resource
    private StoreServiceImpl storeService;
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private ProductGroupServiceImpl productGroupService;

    /**
     * 给用户发放产品
     *
     * @param userId       用户ID
     * @param workerId     产品ID
     * @param storeId      门店ID
     * @param productGroup 产品组
     */
    @Transactional(rollbackFor = Exception.class)
    public void giveProduct(Integer userId,
                            Integer workerId,
                            Integer storeId,
                            ProductGroup productGroup) {
        // 生成订单
        Order order = new Order();
        order.setProductGroupId(productGroup.getId());
        order.setUserId(userId);
        order.setWorkerId(workerId);
        order.setWorkerName(workerService.getWorkerNameById(workerId));
        order.setInviterId(workerId);
        order.setStoreId(storeId);
        order.setStoreName(storeService.getTitleById(storeId));
        order.setType(productGroup.getName());
        order.setPayType(PayTypeEnum.DOUYIN_AUDIT.getDescription());
        order.setPayTime(new Date());
        order.setPayTotal(BigDecimal.ZERO);
        // 价格
        order.setGoodsTotal(new BigDecimal(18));
        order.setOrderNo(orderService.genOrderNo(OrderNoTypeEnum.PRODUCT_GROUP));
        order.setStatus(OrderStatusEnum.complete);
        order.setOrderType(OrderOrderTypeEnum.DOUYIN_WRITE_OFF);
        order.insert();

        // 根据产品组分解产品
        productGroupService.getById(order.getProductGroupId()).map(ProductGroup::getProductGroupDetailList).ifPresent(
                productGroupDetailList -> productGroupDetailList.forEach(productGroupDetail -> {
                    // 0会员卡 1次卡 2权益 3卡包
                    switch (productGroupDetail.getType()) {
                        case 0:
                            productGroupService.memberHandle(order, productGroupDetail.getProductId());
                            break;
                        case 1:
                            productGroupService.timesCardHandle(order, productGroupDetail.getProductId());
                            break;
                        case 2:
                            productGroupService.memberDetailHandle(order, productGroupDetail.getProductId(), CouponCodeOriginEnum.DOUYIN_WRITE_OFF);
                            break;
                        case 3:
                            productGroupService.couponPackageHandle(order, productGroupDetail.getProductId());
                            break;
                        default:
                            break;
                    }
                })
        );
    }
}